package com.foo.demo.stepdefs;

import org.junit.Assert;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.foo.demo.CucumberSpringApplicationTests;
import com.foo.demo.dto.GreetingDTO;

import cucumber.api.java8.En;

@SpringBootTest(classes = CucumberSpringApplicationTests.class)
public class FooStepDefinition implements En {

	@Value("${url}")
	String url;

	public FooStepDefinition() {
		When("the client calls \\/greeting", () -> {
			response = new RestTemplate().getForEntity(url + "/greeting?name=" + name, GreetingDTO.class);
		});
		
		Given("name is {string}", (String name) -> {
			this.name = name;
		});

		Then("the client receives status code of {int}", (Integer statusCode) -> {
			Assert.assertEquals(statusCode.intValue(), response.getStatusCodeValue());
		});

		Then("the client receives content {string}", (String greeting) -> {
			Assert.assertEquals(greeting, response.getBody().getContent());
		});
	}

	String name;

	ResponseEntity<GreetingDTO> response;
}
